// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  //拒绝手机号授权以后提示
  openUserPhoneAuthorize: function () {
    wx.showModal({
      title: '提示',
      content: '功能需要登录后使用，请登录！',
      showCancel: false,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //获取用户手机号码
  getPhoneNumber: function (e) {
    let self = this;
    //异步获取用户信息，所以此处重复判断一遍
    if (app.globalData.userInfo.UserID > 0) {
      wx.navigateTo({
        url: '/pages/ucenter/getUserInfo/getUserInfo',
      });
      return;
    }

    app.checkSession().then(function () {
      let url = app.globalData.apiRootUrl + '/User/AddUserMoile';
      //console.log(e.detail)
      app.httpRequest({
        url: url,
        showLoading: false,
        method: "POST",
        data: {
          SessionKey: app.globalData.userInfo.SessionKey,
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData,
          OpenID: app.globalData.userInfo.OpenID,
          UnionID: app.globalData.userInfo.UnionID,
          UserName: app.globalData.userInfo.NickName,
          UserHead: app.globalData.userInfo.UserHead
        },
        success: function (res) {
          let rdata = res.data;
          //console.log(res);
          if (rdata.status == 0) {
            app.globalData.userInfo.UserID = rdata.data.UserID;
            app.globalData.userInfo.UserMobile = rdata.data.UserMobile;
            app.globalData.userInfo.WeChartUserID = rdata.data.WeChartUserID;
            app.setUserInfo(app.globalData.userInfo);
            wx.navigateTo({
              url: '/pages/ucenter/getUserInfo/getUserInfo',
            });
          } else {
            wx.showToast({
              title: '手机号绑定失败，请重新授权',
              icon: 'none'
            });
          }
        }
      });
    });
  }, 
  conditionsTap: function () {
    let self = this;
    wx.navigateTo({
      url: '/pages/conditions/conditions',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})